package com.swpu.service_knowledge.controller;


import com.swpu.commonutils.JwtUtils;
import com.swpu.commonutils.R;
import com.swpu.service_knowledge.service.ArticleCollectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author chaichai
 * @since 2024-03-27
 */
@RestController
@RequestMapping("/service_knowledge/article-collect")
public class ArticleCollectController {
    @Autowired
    private ArticleCollectService articleCollectService;
    @GetMapping("/addArticleCollect")
    public R addArticleCollect(HttpServletRequest request,@RequestParam("articleId") Integer articleId){
        String openid = JwtUtils.getOpenidByJwtToken(request);
        if (openid.isEmpty()){
            return R.error().message("当前用户未登录");
        }
        articleCollectService.addArticleCollect(openid,articleId);
        return R.ok();
    }
    @GetMapping("/getArticleCollectStatus")
    public R getArticleCollectStatus(HttpServletRequest request,@RequestParam("articleId") Integer articleId){
        String openid = JwtUtils.getOpenidByJwtToken(request);
        if (openid.isEmpty()){
            return R.error().message("当前用户未登录");
        }
        int collectStatus = articleCollectService.getArticleCollectStatus(openid, articleId);
        return R.ok().data("collectStatus",collectStatus);
    }
    @DeleteMapping("/deleteArticleCollect")
    public R deleteArticleCollect(HttpServletRequest request,@RequestParam("articleId") Integer articleId){
        String openid = JwtUtils.getOpenidByJwtToken(request);
        if (openid.isEmpty()){
            return R.error().message("当前用户未登录");
        }
        articleCollectService.deleteArticleCollect(openid,articleId);
        return R.ok();
    }



}

